home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / AIAT / Examples / Sources / PowerPlantStorage / PowerPlantMutex.cp next >
Encoding:
Text File  |  1998-04-16  |  1.1 KB  |  51 lines  |  [TEXT/CWIE]

  1. // PowerPlantMutex.cp
  2. //    Copyright:    © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4.  
  5. #include "PowerPlantMutex.h"
  6. #include <LMutexSemaphore.h>
  7. #include <LThread.h>
  8. #include <UException.h>
  9. #include <UThread.h>
  10.  
  11. class PowerPlantMutex : public IAMutex, public LMutexSemaphore {
  12. public:
  13.     void            Lock();
  14.     void            Unlock();
  15.     void            Signal();
  16. };
  17.  
  18. IAMutex*    NewPowerPlantMutex() {
  19.     return new PowerPlantMutex();
  20. }
  21.  
  22. void PowerPlantMutex::Lock() {
  23.     ThrowIfOSErr_(Wait());
  24. }
  25.  
  26. void PowerPlantMutex::Unlock() {
  27.     Signal();
  28. }
  29.  
  30. void    PowerPlantMutex::Signal() {
  31. // code copied from LMutexSemaphor::Signal(), except w/o call to LThread::Yield()
  32.     LThread        *thread    = LThread::GetCurrentThread();
  33.     {
  34.         StCritical    critical;    // disable preemption within this block
  35.         if (thread != mOwner) {
  36.             Throw_(errSemaphoreNotOwner);
  37.         } else if (mNestedWaits > 0) {
  38.             --mNestedWaits;
  39.         } else if (mExcessSignals < 0) {
  40.             THREAD_ASSERT(mThreads.qHead != NULL);
  41.             mOwner = UnblockThread(mThreads.qHead, noErr);
  42.         } else {
  43.             THREAD_ASSERT(mExcessSignals == 0);
  44.             mOwner = NULL;
  45.             ++mExcessSignals;
  46.         }
  47.     }
  48.     //LThread::Yield();
  49. }
  50.  
  51.